<?php
namespace Home\Controller;
use Think\Controller;
class NewsController extends BaseController{
	public function index(){
		if($_GET['p'] == ''){
			$_GET['p'] = 1;
		}
		$flag = I('flag');
		if($flag == ''){
			$categoryInfo = SearchOne('Category','flag',$THINK.CONTROLLER_NAME);
			$categoryInfo = SearchOne('Category','parent_id',$categoryInfo['id']);
		}else{
			$categoryInfo = SearchOne('Category','flag',$flag);
		}
		$category_id = $categoryInfo['id'];
		$categoryAll = M('Category') -> select();
		$id = getChildsId($categoryAll,$category_id);
		switch($flag){
			case 'weixinshipin':
				$listInfo			= SearchOne('Category','id',$id[0]);
				$map['category_id'] = array('EQ', $id[0]);
				$list				= M('Article') -> where($map) -> order('is_index desc,add_time desc,id desc') -> page($_GET['p'].',7')  -> select();
				$count				= M('Article') -> where($map) -> count();
				$Pages				= new \Think\Pages($count, 7); 
				$Pages -> url		= U('/'.$THINK.CONTROLLER_NAME.'/'.$flag.'/list-'.urlencode('[PAGE]').'.html');
				$show				= $Pages -> show();
				$map['thumb']		= array('NEQ','');
				$photo				= M('Article') -> where($map) -> order('is_index desc,add_time desc,id desc') -> select();
				$videoInfo			= SearchOne('Category','id',$id[1]);
				$video				= M('Article') -> where(array('category_id' => $id[1])) -> order('is_index desc,add_time desc,id desc') -> select();
				$this -> assign('listInfo',$listInfo);
				$this -> assign('video',$video);
				$this -> assign('videoInfo',$videoInfo);
			break;
			default:
				$map['category_id']	= array('EQ',$category_id);
				$list				= M('Article') -> where($map) -> order('is_index desc,add_time desc,id desc') -> page($_GET['p'].',15')  -> select();
				$count				= M('Article') -> where($map) -> count();
				$Pages				= new \Think\Pages($count, 15);
				$Pages -> url		= U('/'.$THINK.CONTROLLER_NAME.'/'.$flag.'/list-'.urlencode('[PAGE]').'.html');
				$show				= $Pages -> show();
				$map['thumb']		= array('NEQ','');
				$photo				= M('Article') -> where($map) -> order('is_index desc,add_time desc,id desc') -> select();
		}
		$this -> assign('flag',$flag);
		$this -> assign('categoryInfo',$categoryInfo);
		$this -> assign('category_id',$category_id);
		$this -> assign('list',$list);
		$this -> assign('pages',$show);
		$this -> assign('photo',$photo);
		$this -> display('news');
	}
	public function view(){
		$id				= I('id');
		$flag			= I('flag');
		$category_id	= M('Category') -> where(array('flag' => $flag)) -> getField('id');
		$categoryInfo	= SearchOne('Category','flag',$flag);
		$articleInfo	= SearchOne('Article','id',$id);
		$front			= M('Article') -> where(array("category_id" => $category_id,"id<".$id)) -> order('id desc') -> limit('1') -> find();  
		if(!$front){
			$f = '上一篇：没有了';
		}else{
			$f = '上一篇：<a href="detail-'.$front["id"].'.html" title="'.$front["title"].'">'.$front["title"].'</a>';
		}
		$after			= M('Article') -> where(array("category_id" => $category_id,"id>".$id)) -> order('id asc') -> limit('1') -> find();  
		if(!$after){
			$a = '下一篇：没有了';
		}else{
			$a = '下一篇：<a href="detail-'.$after["id"].'.html" title="'.$after["title"].'">'.$after["title"].'</a>';
		}

		$this	-> assign('flag',$flag);
		$this	-> assign('category_id',$category_id);
		$this	-> assign('categoryInfo',$categoryInfo);
		$this	-> assign('articleInfo',$articleInfo);
		$this	-> assign('front',$f);
		$this	-> assign('after',$a);
		$this	-> display('view');
	}
}